package com.hod.roommap.client.model;

import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.hod.roommap.client.HOD;


public class BedTypeFormPanel extends HODForm {
	
  	public BedTypeFormPanel(GearsDataStore ds) {
		super(ds);
		// TODO Auto-generated constructor stub
		add(createFieldSet());        
        createButtons();
	}

	protected final NumberField txtidBedType  = new NumberField ("idBedType","idBedType",150); 					 
  	protected final TextField txtdescription  = new TextField ("description","description",150);


	@Override
	protected void createButtons() {
 		 ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		BedType o = null;
              		if (txtidBedType.getText()==""){
              			o = new BedType();                      
              			o.setIdBedType(null);
              		 }else{
              			o = (BedType) ds.findById(new Integer(txtidBedType.getText()));
              		 }                		
                      //metodos para setear el resto de los campos del objeto                       
                      o.setDescription(txtdescription.getValueAsString());
                      ds.save(o);
                      HOD.reload();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		BedType o = (BedType) ds.findById(new Integer(txtidBedType.getText()));
              		ds.delete(o);
              		HOD.reload();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		getForm().reset();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          	           
          
          Button save = new Button("Save", savelistener);
          Button delete = new Button("Delete", deletelistener);
 		  Button newRecord = new Button("New", createlistener);
          Button cancel = new Button("Cancel");
 
          addButton(save);
          addButton(delete);
          addButton(cancel);
          addButton(newRecord);
 		
 	}

	@Override
	protected FieldSet createFieldSet() {
		// TODO Auto-generated method stub
        FieldSet fieldSet = new FieldSet();
        fieldSet.setLabelWidth(80);
        fieldSet.setTitle("BedType");
        fieldSet.setAutoHeight(true);
        fieldSet.setBorder(false);

        //agregar el resto de los campos
        fieldSet.add(txtidBedType);
	    fieldSet.add(txtdescription);
	    
	    txtidBedType.setVisible(false);
	    txtidBedType.setHideLabel(true);
	    
        return fieldSet;
	}

	@Override
	public void updateDataControls(Record record) {
		getForm().loadRecord(record);
	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		
	}

}
